package com.nft.service.school.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nft.constant.response.ResponseConstants;
import com.nft.paging.Paginator;
import com.nft.po.NftResponse;
import com.nft.service.school.entity.SchoolPayrecordEntity;
import com.nft.service.school.entity.SchoolPayrecorddEntity;
import com.nft.service.school.entity.SysUserEntity;
import com.nft.service.school.mapper.SchoolPayrecordMapper;
import com.nft.service.school.mapper.SchoolPayrecorddMapper;
import com.nft.service.school.mapper.SysUserMapper;
import com.nft.service.school.req.SchoolPayrecordReq;
import com.nft.service.school.resp.SchoolPayrecordResp;
import com.nft.service.school.resp.SchoolPayrecorddResp;
import com.nft.service.school.service.SchoolPayrecordService;
import com.nft.utils.query.QueryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SchoolPayrecordServiceImpl extends ServiceImpl<SchoolPayrecordMapper, SchoolPayrecordEntity> implements SchoolPayrecordService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SchoolPayrecorddMapper schoolPayrecorddMapper;

    @Override
    public NftResponse<Paginator<SchoolPayrecorddResp>> getPayrecordList(SchoolPayrecordReq query) {
        if (query.getPageNo() == null || query.getPageNo() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参页码错误", null);
        }
        if (query.getPageSize() == null || query.getPageSize() <= 0) {
            return new NftResponse<>(ResponseConstants.ERROR_CODE, "入参每页数量错误", null);
        }
        QueryWrapper<SysUserEntity> wrapperUser = new QueryWrapper<>();
        wrapperUser.eq(QueryUtil.camelToUnderline("phonenumber"), query.getPhone());
        wrapperUser.eq(QueryUtil.camelToUnderline("delFlag"),"0");
        wrapperUser.eq(QueryUtil.camelToUnderline("status"), "0");
        List<SysUserEntity> users = sysUserMapper.selectList(wrapperUser);
        List<Long> studentIds = new ArrayList<>();
        for (SysUserEntity user:users) {
            studentIds.add(user.getUserId());
        }
        QueryWrapper<SchoolPayrecorddEntity> wrapperQuery = new QueryWrapper<>();
        wrapperQuery.in(QueryUtil.camelToUnderline("studentId"), studentIds);
        wrapperQuery.orderByDesc("create_time");
        Paginator<SchoolPayrecorddResp> paginator = new Paginator<SchoolPayrecorddResp>(query.getPageNo(), query.getPageSize(), 0, new ArrayList<>());
        Page<SchoolPayrecorddEntity> resultPage = schoolPayrecorddMapper.selectPage(new Page<>(query.getPageNo(), query.getPageSize()), wrapperQuery);
        List<SchoolPayrecorddResp> pageResult = BeanUtil.copyToList(resultPage.getRecords(), SchoolPayrecorddResp.class);
        paginator.setPageNo(resultPage.getCurrent());
        paginator.setPageSize(resultPage.getSize());
        paginator.setTotalDataNum(resultPage.getTotal());
        paginator.setPageData(pageResult);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "获取成功", paginator);
    }

    @Override
    public NftResponse<SchoolPayrecordResp> getPayRemainder(String phone) {
        QueryWrapper<SysUserEntity> wrapperUser = new QueryWrapper<>();
        wrapperUser.eq(QueryUtil.camelToUnderline("phonenumber"), phone);
        wrapperUser.eq(QueryUtil.camelToUnderline("delFlag"),"0");
        wrapperUser.eq(QueryUtil.camelToUnderline("status"), "0");
        List<SysUserEntity> users = sysUserMapper.selectList(wrapperUser);
        List<Long> studentIds = new ArrayList<>();
        for (SysUserEntity user:users) {
            studentIds.add(user.getUserId());
        }
        SchoolPayrecordResp rtn = this.baseMapper.getUserRemainder(studentIds);
        return new NftResponse<>(ResponseConstants.SUCCESS_CODE, "查询成功", BeanUtil.copyProperties(rtn, SchoolPayrecordResp.class));
    }
}
